<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :visible.sync="dialog" :title="isAdd ? '新增字典' : '修改字典'" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="160px">
      <el-form-item label="类别" prop="busMainData.dictType">
        <el-input v-model="form.busMainData.dictType" style="width: 300px;"/>
      </el-form-item>
      <el-form-item label="字典值" prop="busMainData.dictCode">
        <el-input v-model="form.busMainData.dictCode" style="width: 300px;"/>
      </el-form-item>
      <el-form-item label="名称" prop="busMainData.dictName">
        <el-input v-model="form.busMainData.dictName" style="width: 300px;"/>
      </el-form-item>
      <el-form-item label="排序" prop="busMainData.sort">
        <el-input-number v-model.number="form.busMainData.sort" :min="0" :max="999" controls-position="right" style="width: 300px;"/>
      </el-form-item>
      <el-form-item label="语言" prop="busMainData.lanCode">
        <el-select v-model="form.busMainData.lanCode" :style="style" placeholder="请选语言">
          <el-option
            v-for="(item) in dicts.lan_code"
            :key="item.id"
            :label="item.dictName"
            :value="item.dictCode"/>
        </el-select>
      </el-form-item>
      <el-form-item label="是否有效" prop="busMainData.enableFlag">
        <el-radio v-for="item in dicts.enable_flag" :key="item.id" v-model="form.busMainData.enableFlag" :label="item.dictCode">{{ item.dictName }}</el-radio>
      </el-form-item>
      <el-form-item label="扩展属性一" prop="busMainData.attribute1">
        <el-input v-model="form.busMainData.attribute1" style="width: 300px;"/>
      </el-form-item>
      <el-form-item label="扩展属性二" prop="busMainData.attribute2">
        <el-input v-model="form.busMainData.attribute2" style="width: 300px;"/>
      </el-form-item>
      <el-form-item label="扩展属性三" prop="busMainData.attribute3">
        <el-input v-model="form.busMainData.attribute3" style="width: 300px;"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN']" :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addOrEdit, viewMainData, toAdd, toCopy } from '@/api/system/dataDict'

export default {
  components: { },
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  dictTypes: ['lan_code', 'enable_flag'],
  data() {
    return {
      isAdd: false,
      loading: false,
      dialog: false,
      style: 'width: 184px',
      form: {
        busMainData: {
          id: '',
          dictType: '',
          dictCode: '',
          dictName: '',
          sort: 1,
          lanCode: 'zh_CN',
          enableFlag: 'Y'
        }
      },
      rules: {
        'busMainData.dictType': [
          { required: true, message: '请输入类别', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        'busMainData.dictCode': [
          { required: true, message: '字典值', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        'busMainData.dictName': [
          { required: true, message: '名称', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
    },
    addData() {
      this.isAdd = true
      const data = { paramBean: { }}
      toAdd(data).then(res => {
        this.dialog = true
        this.form = res.data
      }).catch(err => {
        this.dialog = false
        console.log(err)
      })
    },
    copyData(mainId) {
      const data = { paramBean: { id: mainId }}
      toCopy(data).then(res => {
        this.dialog = true
        this.form = res.data
      }).catch(err => {
        this.dialog = false
        console.log(err)
      })
    },
    viewMainData(id) {
      const data = { paramBean: { id: id }}
      viewMainData(data).then(res => {
        this.form = res.data
      })
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        }
      })
    },
    doAdd() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
